﻿using Sofia.Models;
using Sofia.Web.ViewModel;
using Sofia.Web.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Sofia.Web.Controllers
{
    public class HomeController : Controller
    {
        SofiaDb db = new SofiaDb();


        public ActionResult Index()
        {
            return View();
        }


        public ActionResult Corps()
        {
            var lastCorpPost = db.Corps
                .OrderByDescending(x => x.CreationTime)
                .Take(15)
                .Select(x => new CorpViewModel
                {
                  Title = x.Title,
                  Description = x.Description,
                  CreationTime = x.CreationTime,
                  username = x.userName.UserName,
                });
                    
            return View(lastCorpPost);
        }

        public ActionResult About()
        {
            var lastProgramPost = db.Programs
                .OrderByDescending(x => x.CreationTime)
                .Take(15)
                .Select(x => new programViewModel
                {
                    Title = x.Title,
                    Description = x.Description,
                    CreationTime = x.CreationTime,
                    username = x.UserName.UserName,
                });
            return View(lastProgramPost);
        }

        public ActionResult Market()
        {
            var marketLastPost = db.Markets
                .OrderByDescending(x => x.CreationTime)
                .Take(15)
                .Select(x => new MarketViewModel
                {
                    Title = x.Title,
                    Description = x.Description,
                    CreationTime = x.CreationTime,
                    userName = x.userName.UserName,
                });
            return View();
        }

        public ActionResult Love()
        {
            var loveLastPost = db.Loves
                .OrderByDescending(x => x.CreationTime)
                .Take(15)
                .Select(x => new LoveViewModel
                {
                    Title = x.Title,
                    Description = x.Description,
                    CreationTime = x.CreationTime,
                    UserName = x.UserName.UserName,
                });
            return View(loveLastPost);
        }

        public ActionResult SysAdmin()
        {
            var SysAdminLastPost = db.SysAdmins
                .OrderByDescending(x => x.CreationTime)
                .Take(15)
                .Select(x => new SysAdminViewModel
                {
                    Title = x.Title,
                    Description = x.Description,
                    Username = x.UserName.UserName,
                    CreationTime = x.CreationTime,
                });
            return View(SysAdminLastPost);
        }

        public ActionResult Manage()
        {
            var manageLastPost = db.Manages
                .OrderByDescending(x => x.CreationTime)
                .Take(15)
                .Select(x => new manageViewModel
                {
                    Title = x.Title,
                    Description = x.Description,
                    CreationTime = x.CreationTime,
                    userName = x.userName.UserName,
                });
            return View(manageLastPost);
        }

        public ActionResult Story()
        {
            var storyInfo = db.Storys
                .OrderByDescending(x => x.CreationTime)
                .Take(15)
                .Select(x => new StoryViewModel
                {
                    Title = x.Title,
                    Description = x.Description,
                    CreationTime = x.CreationTime,
                    userName = x.UserName.UserName,
                });
            return View(storyInfo);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
